/**
 * ReportGear(2011)
 */
package net.abbp.report.utils;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import net.abbp.report.model.ReportModel;
import net.abbp.report.view.GridView;

import com.reportgear.report.model.auxiliary.ColumnPropList;
import com.reportgear.report.model.auxiliary.ColumnRow;
import com.reportgear.report.model.auxiliary.RowPropList;
import com.reportgear.report.view.grid.selection.GridSelection;
import com.reportgear.report.view.grid.selection.Selection;

/**
 * 报表实用类
 * 
 * @version 1.0 2011-5-8
 * @author <a herf="lexloo@gmail.com">lexloo</a>
 * @since Report 1.0
 */
public class ReportUtils {
    // 字母总数
    private static final int LETTER_NUM = 26;
    // 字母表
    private static final char[] digits = { 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O',
            'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z' };

    /**
     * 转换整数到字母代码 1-A,2-B
     * 
     * @param index
     *            所有
     * @return 转换后字符串
     */
    public static String convertIntToABC(int index) {
        int pos = index;
        StringBuilder sb = new StringBuilder();

        if (pos == 0) {
            return "";
        }

        while (pos != 0) {
            int inPos = pos % LETTER_NUM;
            if (inPos == 0) {
                inPos = LETTER_NUM;
            }

            sb.insert(0, digits[(inPos - 1)]);

            pos = (pos - inPos) / LETTER_NUM;
        }

        return sb.toString();
    }

    /**
     * ABC字符串转化成整型值
     * 
     * @param absStr
     *            abc字符串
     * @return 代表的整数
     */
    public static int convertABCToInt(String absStr) {
        int ret = 0;
        absStr = absStr.toUpperCase();

        for (int i = 0, len = absStr.length(); i < len; ++i) {
            ret = Character.getNumericValue(absStr.charAt(i)) - Character.getNumericValue('A') + LETTER_NUM * ret + 1;
        }

        return ret;
    }

    /**
     * 获取网格选择行Index列表
     * 
     * @param gridSelection
     *            网格选择集
     * @return 行Index列表
     */
    // TODO 改为SET
    public static List<Integer> getGridSelectionRowIndex(GridSelection gridSelection) {
        List<Integer> result = new ArrayList<Integer>();
        for (Selection sel : gridSelection.getCellSelectionList()) {
            int row = sel.getRow();
            for (int j = 0, len = sel.getRowSpan(); j < len; j++) {
                int k = row + j;
                if (result.contains(k)) {
                    continue;
                }

                result.add(k);
            }
        }

        Collections.sort(result);

        return result;
    }

    /**
     * 获取网格选择列Index列表
     * 
     * @param gridSelection
     *            网格选择集
     * @return 列Index列表
     */
    // TODO 改为SET
    public static List<Integer> getGridSelectionColumnIndex(GridSelection gridSelection) {
        List<Integer> result = new ArrayList<Integer>();
        for (Selection sel : gridSelection.getCellSelectionList()) {
            int column = sel.getColumn();
            for (int j = 0, len = sel.getColumSpan(); j < len; ++j) {
                int k = column + j;
                if (result.contains(k)) {
                    continue;
                }

                result.add(k);
            }
        }
        Collections.sort(result);

        return result;
    }

    public static String convertColumnRowToCellString(ColumnRow columnRow) {
        if (columnRow == null) {
            return "";
        }
        return columnRow.toString();
    }

    public static ColumnRow convertCellStringToColumnRow(String cellString) {
        return ColumnRow.valueOf(cellString);
    }

    public static String addFileSeparator(String str) {
        if ((!(str.endsWith("/"))) && (!(str.endsWith("\\")))) {
            return str += File.separator;
        }
        return str;
    }

    public static byte[] getBytesFromIS(InputStream inputStream) throws IOException {
        ByteArrayOutputStream arrayOutputStream = new ByteArrayOutputStream();
        int i = 0;
        while ((i = inputStream.read()) != -1) {
            arrayOutputStream.write(i);
        }
        return arrayOutputStream.toByteArray();
    }

    /**
     * 
     * 根据X、Y坐标获取ColumnRow
     * 
     * @param gridView 工作簿
     * @param x x 坐标
     * @param y y 坐标
     * @return
     */
    public static ColumnRow getColumnRowByXY(GridView gridView, double x, double y) {
        ReportModel reportModel = gridView.getReportModel();

        RowPropList rpl = reportModel.getRowPropList();
        ColumnPropList cpl = reportModel.getColumnPropList();

        double tmpValue = 0.0D;

        // 获取X索引
        int xx = gridView.getHoriBeginValue();
        int columnMax = gridView.getMaxColumnCount() - 1;
        while (xx < columnMax) {
            tmpValue += cpl.getWidth(xx);
            if (tmpValue > x) {
                break;
            }
            xx++;
        }

        // 计算Y索引
        tmpValue = 0.0D;
        int yy = gridView.getVertBeginValue();
        int rowMax = gridView.getMaxRowCount() - 1;
        while (yy < rowMax) {
            tmpValue += rpl.getHeight(yy);
            if (tmpValue > y) {
                break;
            }
            yy++;
        }

        return ColumnRow.valueOf(xx, yy);
    }

    /**
     * 根据 x，y位置在Sheet中查找单元格
     * 
     * @param gridView 工作薄
     * @param x x位置
     * @param y y位置
     * @return ColumnRow对象
     */
    public static ColumnRow getAdjustEventColumnRow(GridView gridView, double x, double y) {
        ColumnRow columnRow = getColumnRowByXY(gridView, x, y);
        int column = Math.max(columnRow.getColumn(), 0);
        int row = Math.max(columnRow.getRow(), 0);

        return ColumnRow.valueOf(column, row);
    }
}
